<?php
//前台
class IndexControl extends PublicControl{
    //模型数据
    private $model;
    //自动运行
    public function __auto()
    {
        $this->model = K('User');
    }

    /**
     * [index 首页]
     * @return [type] [description]
     */
    public function index()
    {
      if(!empty($_GET['code'])&&!empty($_GET['state']))
      {
         //QQ回调 数据
        $qc = new QC();
        // 获得令牌AccessToken
        $acs= $qc->qq_callback();
        // 获得唯一标识用户id，用于存入数据库当做用户名
        $oid= $qc->get_openid();
        // 传递令牌与用户唯一标识，获得用户信息 
        $qc = new QC($acs,$oid); 
        $userInfo= $qc->get_user_info(); 
        $userDate = K('User')->get_all(array('oid'=>$oid),array('oid','nickname','uid'));
        if($result)
        {
           // session('username',$userDate['username']);
            session('uid',$userDate['uid']);
           session('nickname',$userDate['nickname']) ;
           go('index');
        }else{
          $data = array(
            //QQ数据中的NICKNAME
            'nickname' => $userInfo['nickname'],
            //QQ用户的唯王一标识
            'oid'=>$oid,

            );
          $uid = K('User')->add_data($data);
          session('uid',$uid);
           session('nickname',$userInfo['nickname']);

            go('index');
        }

        // p($userInfo);die;
      }
     //============================================排行=======================
     $this->goods_panhan();

//========================================BANNER广告数据==============================================
        $bannerdata = k('Ad')->get_all();
      $this->assign('bannerdata',$bannerdata);
//========================================首页在线选座++++++++++++++++++++++++++++++++++++
        //对数组重组，取需要的数据
        $seatgoodsData = k('Goods')->get_all(array('is_seat'=>1),'goodsid desc');
        //+++++++++++++++++++取出最小的价格++++++++++++++++++++++++//
        foreach ($seatgoodsData as $key => $value) 
        {
            // 找出相应的价格
           $goodstimeid =  K('Goodstime')->get_one(array('goodsid'=>$value['goodsid']),'timeid'); 
           //取出所有的价格。
           $price = K('Goodsprice')->get_all(array('goodstimeid'=>$goodstimeid));
           // 取出数组中PRICE最小的数
           foreach ($price as $km => $vm) {
              if($km==0){
                //第一次循环，先把VM值付给MIN
                $min = $vm['price'];
              }
              if($vm['price']<$min){
                $min = $vm['price'];
              }
           }
          //+++++++++++++++++++取出图片的路径++++++++++++++++++++++++//
            // 通过GOODSID查出商品的GOODSDES
            $goodsimage = K('Goodsdes')->get_one(array('goodsid'=>$value['goodsid']),'goodsimage');
            // p($goodsimage);
            // 将得到的最小价格和图片路径符值给GOODSDATA数组，好分配;
           $seatgoodsData[$key]['minprice']=$min;
           $seatgoodsData[$key]['goodsimage']=$goodsimage['goodsimage'];
        }
        // P($seatgoodsData);
    //分配数据
       $this->assign('seatgoodsData',$seatgoodsData);
//========================================商品数据<首页的多少元起>++++++++++++++++++++++++++++++++++++
        //对数组重组，取需要的数据
        $goodsData = k('Goods')->get_all(null,'goodsid desc');
        // p($goodsData);
        //+++++++++++++++++++取出最小的价格++++++++++++++++++++++++//
        foreach ($goodsData as $key => $value) 
        {
            // 找出相应的价格
           $goodstimeid =  K('Goodstime')->get_one(array('goodsid'=>$value['goodsid']),'timeid'); 
           //取出所有的价格。
           $price = K('Goodsprice')->get_all(array('goodstimeid'=>$goodstimeid));
           // 取出数组中PRICE最小的数
           foreach ($price as $km => $vm) {
              if($km==0){
                //第一次循环，先把VM值付给MIN
                $min = $vm['price'];
              }
              if($vm['price']<$min){
                $min = $vm['price'];
              }
           }
            //+++++++++++++++++++取出图片的路径++++++++++++++++++++++++//
            // 通过GOODSID查出商品的GOODSDES
            $goodsimage = K('Goodsdes')->get_one(array('goodsid'=>$value['goodsid']),'goodsimage');
            // p($goodsimage);
            // 将得到的最小价格和图片路径符值给GOODSDATA数组，好分配;
           $goodsData[$key]['minprice']=$min;
           $goodsData[$key]['goodsimage']=$goodsimage['goodsimage'];
        }
        // P($goodsData);
    //分配数据
       $this->assign('goodsData',$goodsData);
//========================================商品数据<演唱会CATEID=2>++++++++++++++++++++++++++++++++++++
         //先找出常演唱会下面的子级分类
         $cateData = K('Category')->get_all(array('pid'=>2),'cateid');
         // p($cateData);
         $cateid = array();
         foreach ($cateData as $k => $v) {
          //根据找出的CATEID查出商品数据
          $cateid[]=$v['cateid'];
          // p($CgoodsData);
         }
         $allCtaeData = K('Goods')->get_in(array('cateid'=>$cateid),'goodsid desc',array('goodsid','title'));
         // p($allCtaeData);
         $goodsid=array();
        foreach ($allCtaeData as $key => $value) 
        {
          $goodsid[]=$value['goodsid'];
        }
         $desData = K('Goodsdes')->get_in(array('goodsid'=>$goodsid),'goodsid desc',array('goodsimage'));
         // p($desData);
        foreach ($allCtaeData as $k => $v) {
          // p($desData[$k]['goodsimage']);
          $allCtaeData[$k]['goodsimage'] = $desData[$k]['goodsimage'];
        }
        // p($allCtaeData);
        $this->assign('allCtaeData',$allCtaeData);

//========================================商品数据<话剧歌剧=2>++++++++++++++++++++++++++++++++++++
///先找出常演唱会下面的子级分类
         $cateData = K('Category')->get_all(array('pid'=>9),'cateid');
         // p($cateData);
         $cateid = array();
         foreach ($cateData as $k => $v) {
          //根据找出的CATEID查出商品数据
          $cateid[]=$v['cateid'];
          // p($CgoodsData);
         }
         $allCtaeData = K('Goods')->get_in(array('cateid'=>$cateid),'goodsid desc',array('goodsid','title'));
         $goodsid=array();
        foreach ($allCtaeData as $key => $value) 
        {
          $goodsid[]=$value['goodsid'];
        }
         $desData = K('Goodsdes')->get_in(array('goodsid'=>$goodsid),'goodsid desc',array('goodsimage'));
         // p($desData);
        foreach ($allCtaeData as $k => $v) {
          // p($desData[$k]['goodsimage']);
          $allCtaeData[$k]['goodsimage'] = $desData[$k]['goodsimage'];
        }
        // p($allCtaeData);
        $this->assign('gegu',$allCtaeData);
  	  $this->display();
    }

    /**
     * [login 前台登陆]
     * @return [type] [description]
     */
    public function login()
    {
        if(IS_POST){
            //验证登陆
            $userDate = $this->model->validate_index_login();
            if(!$userDate)
            {
                $this->error($this->model->error,U('index'),20);

            }
            session('username',$userDate['username']);
            session('uid',$userDate['uid']);
            $result = current($this->model->get_all(array('uid'=>session('uid')),array('nickname')));
           session('nickname',$result['nickname']) ;
           //检查下，SEESION中是否有商品，如果有，就把SESSION中的商品加入到数据库
           if(isset($_SESSION['shopcar'])){
                if($_SESSION['shopcar']){
                  foreach ($_SESSION['shopcar'] as $key => $value) {
                    $shopcardata = array(
                        'goodsid'=>$value['goodsid'],
                        'timeid'=>$value['timeid'],
                        'number'=>$value['number'],
                        'goodsprice'=>$value['goodsprice'],
                        'uid'=>session('uid')
                        );
                   $result = K('Shopcar')->add_($shopcardata);
                   if($result){
                    $_SESSION['shopcar']=null;
                   }

                }
               }
           }
           
           //检测SESSION中有没有URL键接，如果有，就跳转到URL，没有就跳至首页
           isset($_SESSION['gourl']) ? $this->success('登陆成功',session('gourl')): $this->success('登陆成功',U('Index/index'));
        }

    	$this->display();
    }
    public function log_out()
    {
        session('username',null);
        session('nickname',null);
        session('uid',null);
        $this->success('退出成功',U('index'));
    }
    /**
     * [reg 前台注册]
     * @return [type] [description]
     */
    public function reg()
    {
        if(IS_POST)
        {

            $code = strtoupper($_SESSION['code']);
            if(Q('post.code','','strtoupper')!=$code)
            {
                $this->error('验证码错误');
            }
           $data = Q('post.');
           //MD5加密
           $data['password'] = md5($data['password']);
            $this->model->add_data($data);
            $this->success('注册成功',U('login'));
        }
    	$this->display();
    }
    /**
     * [code 验证码]
     * @return [type] [description]
     */
    public function code()
    {
    	$code = new Code;
    	$code->show();

    }
    /**
     * [ajx_check_code 异步验证CODE《验证码》]
     * @return [type] [description]
     */
    public function ajx_check_code()
    {
        if(IS_AJAX)
        {
            $code = Q('post.code','','strtoupper');
            if($_SESSION['code']==$code)
            {
                echo '1';die;
            }
            echo '0'; die;
        }
    }
    /**
     * [qq_login QQ登陆]
     * @return [type] [description]
     */
    public function qqlogin()
    {
       $qc = new QC(); 
       $qc->qq_login();
    }
    /**
     * [sinalogin 新浪登陆]
     * @return [type] [description]
     */
    public function sinalogin()
    {
      include_once( ROOT_PATH.'/sina/config.php' );
     include_once( ROOT_PATH.'/sina/saetv2.ex.class.php' );
      $o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
      $code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
    echo "<script>location.href='$code_url?';</script>"; 
    }
  /**
  * [panhan 首页商品排行]
  * @return [type] [description]
  */
public function goods_panhan()
{
  //演唱会排行
   $cateData = K('Category')->get_all(array('pid'=>2),'cateid');
           // p($cateData);
           $cateid = array();
           if(is_array($cateData)){
            foreach ($cateData as $k => $v) {
            //根据找出的CATEID查出商品数据
            // p($v);
             $cateid[]=$v['cateid'];
            // p($cateid);
            }
           }
  //演唱会数据
  $yancuanData = K('Goods')->get_in(array('cateid'=>$cateid),'click desc',array('goodsid','title','cinemaid','startime','overtime','goodsname','click'),5);
  //分配数据
    $this->assign('yancuanData',$yancuanData);
           // p($yancuanData);
           
  //音乐会排行
   $cateData = K('Category')->get_all(array('pid'=>8),'cateid');
           // p($cateData);
           $cateid = array();
           if(is_array($cateData)){
            foreach ($cateData as $k => $v) {
            //根据找出的CATEID查出商品数据
            // p($v);
             $cateid[]=$v['cateid'];
            // p($cateid);
            }
           }
  //音乐会数据
  $MusicData = K('Goods')->get_in(array('cateid'=>$cateid),'click desc',array('goodsid','title','cinemaid','startime','overtime','goodsname','click'),5);
  //分配数据
    $this->assign('MusicData',$MusicData);
           // p($MusicData);
    //话剧排行
   $cateData = K('Category')->get_all(array('pid'=>9),'cateid');
           // p($cateData);
           $cateid = array();
           if(is_array($cateData)){
            foreach ($cateData as $k => $v) {
            //根据找出的CATEID查出商品数据
            // p($v);
             $cateid[]=$v['cateid'];
            // p($cateid);
            }
           }
  //话剧数据
  $FajuData = K('Goods')->get_in(array('cateid'=>$cateid),'click desc',array('goodsid','title','cinemaid','startime','overtime','goodsname','click'),5);
  //分配数据
    $this->assign('FajuData',$FajuData);
    // p($FajuData);
}
}
?>